const axios = require('axios');
const {getValueByCommandKey} = require("./command");

// const host = getValueByCommandKey('--api') || '127.0.0.1:3000';

const BASE_URL = process.env.API || 'http://127.0.0.1:3000';


// 创建axios实例
const service = axios.create({
    baseURL: BASE_URL,
    timeout: 3 * 1000,
    responseType: "json",
    responseEncoding: "utf8",
})


// 请求拦截器
service.interceptors.request.use(function (config) {
    console.log('本次请求路径:', config.url);
    // console.log(config)
    return config;
}, function (error) {
    console.log(error)
    Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(function (response) {
    const code = response.status;
    if (code === 200) {
        return response.data;
    } else {
        return Promise.reject({
            message: res.message || 'Error',
            status: res.code
        });
    }
}, function (error) {
    console.log('响应错误:' + error);
    return Promise.reject(error);
})

module.exports = {
    request: service
}